#ifndef _MUXER_MP4_H_
#define _MUXER_MP4_H_

#include "ringbuffer.hpp"
#include <cstring>
#include <iostream>
#include <queue>
#include <thread>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
}

enum command {
    COMMAND_IDLE = 0,
    COMMAND_START,
    COMMAND_STOP,
    COMMAND_MAX
};

enum state {
    STATE_IDLE = 0,
    STATE_WORK,
    STATE_MAX
};

class muxer_mp4 {
private:
    std::string ip;
    bool running;
    std::thread handle;
    ringbuffer* rbuf;
    command cmd;
    int64_t pts;
    AVFormatContext* fmtctx;
    AVStream* stream;
    AVCodec* codec;
    std::string filename;

public:
    muxer_mp4();
    muxer_mp4(ringbuffer* ringbuffer, std::string ip);
    ~muxer_mp4();

    int set(ringbuffer* ringbuffer, std::string ip);
    void thread();
    void start();
    void stop();
    int cut(command cmd, int64_t pts);
    int open(std::string filename);
    int write(AVPacket* packet);
    int close();
    void msleep(int ms);
};

#endif